<template>
  <div class="home">
    <el-container>
      <el-header>
        <div>
          <img src="../assets/ITheima_logo.png" alt="" />
          <span>电商后台管理系统 </span>
        </div>
        <el-button @click="Home_out" type="info">退出</el-button>
      </el-header>
      <el-container>
        <el-aside :width="isCollapse ? '64px' : '200px'">
          <el-menu
            class="el-menu-vertical-demo"
            background-color="#333744"
            text-color="#fff"
            active-text-color="rgb(64, 158, 255)"
            unique-opened
            :collapse="isCollapse"
            :collapse-transition="false"
            router
            :default-active="defActivePath"
          >
            <el-submenu :index="v.path" v-for="v in menus" :key="v.id">
              <template #title>
                <i :class="icon[v.id]"></i>
                <span>{{ v.authName }}</span>
              </template>
              <template #default>
                <el-menu-item :index="v1.path" v-for="v1 in v.children" :key="v1.id">
                  <template #default>
                    <i class="el-icon-menu"></i>
                  </template>
                  <template #title>
                    <span>{{ v1.authName }}</span>
                  </template>
                </el-menu-item>
              </template>
            </el-submenu>
          </el-menu>
          <div class="folding" @click="isFolding">
            <i :class="isCollapse ? 'el-icon-arrow-right' : 'el-icon-arrow-left'"></i>
          </div>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  name: 'Home',
  data() {
    return {
      // defActivePath: location.hash.slice(2),
      menus: [],
      icon: {
        125: 'el-icon-user-solid',
        103: 'el-icon-s-platform',
        101: 'el-icon-s-goods',
        102: 'el-icon-s-claim',
        145: 'el-icon-s-data',
      },
      isCollapse: false,
    };
  },
  computed: {
    defActivePath() {
      return this.$route.path.slice(1);
    },
  },
  methods: {
    Home_out() {
      this.$confirm('确认退出吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        this.$message({
          type: 'success',
          message: '退出成功!',
        });
        localStorage.removeItem('token');
        this.$router.push('/login');
      });
    },
    isFolding() {
      this.isCollapse = !this.isCollapse;
    },
  },

  created() {
    this.request.getLeftMenus().then((res) => {
      if (res.data.meta.status !== 200) return this.$message.error(res.data.meta.msg);
      this.menus = res.data.data;
      // console.log(res.data.data);
    });
  },
};
</script>

<style lang="less" scoped>
.home {
  .el-container {
    height: 100vh;
    .el-header {
      background-color: #373d41;
      display: flex;
      justify-content: space-between;
      align-items: center;
      div {
        display: flex;
        align-items: center;
        img {
          height: 60px;
        }
        span {
          margin-left: 20px;
          font-size: 24px;
          color: #fff;
        }
      }
    }
    .el-container {
      .el-aside {
        position: relative;
        background-color: #333744;
        .el-menu-vertical-demo {
          border-right: 0;
        }
        .folding {
          position: absolute;
          bottom: 0;
          display: flex;
          justify-content: center;
          align-items: center;
          width: 100%;
          height: 60px;
          color: #fff;
          font-size: 24px;
        }
        .folding:hover {
          // transform: rotateY(180deg);
          // transition: linear 0.2s;
          background-color: rgba(0, 0, 0, 0.2);
        }
      }
      .el-main {
        background-color: #eaedf1;
      }
    }
  }
}
</style>
